﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MarvellousWorks.PracticalPatternNet8.Concept.Indexer;

public class Dashboard
{
    float[] temps = new float[10] {56.2F, 57.3F, 58.4F, 59.5F, 60.6F, 61.7F, 62.8F, 63.9F, 64.0F, 65.1F};

    /// <summary>
    /// 与 SQL 语句中的 where 子句类似的索引器。
    /// </summary>
    /// <param name="predicate">传入的检索规则</param>
    /// <returns></returns>
    public float this[Predicate<float> predicate]
    {
        get
        {
            for (int i = 0; i < temps.Length; i++)
            {
                if (predicate(temps[i]))
                {
                    return temps[i];
                }
            }
            throw new ArgumentException("No temperature matches the given predicate.");
            //float[] matches = temps.Where(temp => predicate(temp)).ToArray();
            //return matches[0];
        }
    }

    public float this[params Predicate<float>[] predicates]
    {
        get
        {
            // 使用 LINQ 的 Aggregate 方法来处理多个谓词
            return temps.Where(temp => predicates.All(predicate => predicate(temp))).FirstOrDefault();
        }
    }
}
